local storm = require 'jass.storm'
local japi = require 'jass.japi'
local dump = require 'util.dump'

local storm_load = storm.load 
local storm_save = storm.save

local signname = "SoulWolf"
local files = "scripts.core.types.record.files"

-- 映射器 
local mapper = { }
local ok, res = pcall(require, files)
if ok and res and type(res) == 'table' and res['1__sign：'] == signname then 
    mapper = res 
end

local m = {} 

local mapper_type = {
    ['boolean'] = true,
    ['integer'] = true,
    ['archive'] = true,
    ['isscore'] = true,
}

function m.define(name, key)
    if mapper_type[name] then 
        local data = mapper[name]
        if not data then 
            data = { index = 0, mapping = { }, }
            mapper[name] = data
        end 
        if not data.mapping[key] then 
            data.index = data.index + 1 
            data.mapping[key] = data.index
        end
        return data.mapping[key]
    end
end

function m.index(name, key)
    if mapper_type[name] then 
        local data = mapper[name]
        if data and data.mapping then 
            return data.mapping[key]
        end 
    end
end 

function m.amount(name)
    if mapper_type[name] then 
        local data = mapper[name]
        if not data then 
            return 0
        end 
        return data.index
    end 
    return 0
end 

function m.save()
    if package.local_test then 
        mapper['1__sign：'] = signname
        mapper['2__warn：'] = "本文件由系统自动生成"
        mapper['3__warn：'] = "用途为支持新增存档顺序可任意打乱"
        mapper['4__warn：'] = "请勿改动！请勿改动！请勿改动！重要的事情说三遍！"
        storm_save(package.local_map_path .. (files):gsub("%.", '\\') .. ".lua", 'return '.. dump(mapper))
    end 
end

return m